/**
 * 
 */
Ext.ns("examineeApply");
examineeApply.init = function() {
	examineeApply.big_Id;// 定义选定的大类型的序列号
	examineeApply.big_Name;// 定义选定的大类型名称
	examineeApply.small_Id;// 定义选定的小类型的序列号
	examineeApply.small_Name;// 定义选定的大类型名称
	examineeApply.exam_Id;
	examineeApply.exam_Name;
	examineeApply.startTime;
	examineeApply.endTime;
	examineeApply.menuClick;
	examineeApply.unit_Id;
	examineeApply.autoArrange = false;// 判断是否已经自动安排了考生
	examineeApply.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : '申办学校或单位',
				dataIndex : 'name',
				sortable : true
			}, {
				header : '城市',
				dataIndex : 'city',
				sortable : true
			}, {
				header : '地区',
				dataIndex : 'region',
				sortable : true
			}, {
				header : '分配人数',
				dataIndex : 'personCount',
				sortable : true
			}, {
				header : '状态',
				dataIndex : 'state',
				renderer : function(value, celmeta, record) {
					var a = record.data["state"];
					if (a == 0) {
						return "未分配";
					} else {
						return "已分配";
					}
				},
				sortable : true
			}]);
	examineeApply.lcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
				header : '考试名称',
				dataIndex : 'l_name',
				sortable : true,
				width : 30
			}, {
				header : '考试时间',
				dataIndex : 'l_time',
				renderer : Ext.util.Format.dateRenderer('Y年m月d日   h时i分'),
				sortable : true,
				width : 20
			}]);
	examineeApply.lstore = new Ext.data.JsonStore({
				url : 'center/showExamListForMonitorAndExaminee.action',
				totalProperty : 'total',
				root : 'examValues',
				baseParams : {
					status : 2
					// 查询监考人员分配完成相关考试项目
				},
				fields : [{
							name : "l_id",
							mapping : 'examInfo_Id',
							type : 'int'
						}, {
							name : "l_name",
							mapping : 'examInfo_Name',
							type : 'string'
						}, {
							name : "l_time",
							mapping : 'examInfo_StartTime',
							type : 'string'
						}, {
							name : "l_etime",
							mapping : 'examInfo_EndTime',
							type : 'string'
						}]
			});
	examineeApply.store = new Ext.data.JsonStore({
				url : 'center/showConductionForExaminee.action',
				totalProperty : 'total',
				root : 'conductions',
				fields : [{
							name : "id",
							mapping : 'id',
							type : 'int'
						}, {
							name : "epq_Id",
							mapping : 'epq_Id',
							type : 'int'
						}, {
							name : "name",
							mapping : 'name',
							type : 'string'
						}, {
							name : "city",
							mapping : 'city',
							type : 'string'
						}, {
							name : "region",
							mapping : 'region',
							type : 'string'
						}, {
							name : "personCount",
							mapping : 'personCount',
							type : 'int'
						}, {
							name : "state",
							mapping : 'state',
							type : 'string'
						}]
			});

	examineeApply.BigTypes = new Ext.data.JsonStore({
				url : 'public/showBigList.action',
				totalProperty : 'total',
				root : 'bigtypes',
				fields : [{
							name : "big_Id",
							type : 'int'
						}, {
							name : "big_Name",
							type : 'string'
						}]
			});
	examineeApply.SmallTypes = new Ext.data.JsonStore({
				url : 'public/showSmallList.action',
				totalProperty : 'total',
				root : 'smalltypes',
				baseParams : {
					start : 0
				},
				fields : [{
							name : "small_Id",
							mapping : 'small_Id',
							type : 'int'
						}, {
							name : "small_Name",
							mapping : 'small_Name',
							type : 'string'
						}]
			});
	examineeApply.LPanel = new Ext.grid.GridPanel({
				columnWidth : .35,
				cm : examineeApply.lcm,
				store : examineeApply.lstore,
				frame : true,
				loadMask : true,
				height : 480,
				autoScroll:true,
				viewConfig : {
					columnsText : '选择显示列',
					forceFit : true
				},
				tbar : new Ext.Toolbar(['-', {
							id : 'examineeApply_search_bigType',
							fieldLabel : '考试类型',
							allowBlank : false,
							emptyText : '请选择类型名查询',
							blankText : '对不起，请选择考试类型',
							xtype : 'combo',
							mode : 'remote',
							triggerAction : 'all',
							valueField : 'big_Id',
							displayField : 'big_Name',
							pageSize : 8,
							width : 120,
							minListWidth : 300,
							store : examineeApply.BigTypes
						}, '-', {
							id : 'examineeApply_search_smallType',
							fieldLabel : '考试类型细节',
							allowBlank : false,
							emptyText : '请选择类型细节',
							blankText : '对不起，请选择类型细节进行查询',
							name : 'examValue.exam_Type',
							xtype : 'combo',
							mode : 'local',
							triggerAction : 'all',
							valueField : 'small_Id',
							displayField : 'small_Name',
							pageSize : 8,
							width : 120,
							minListWidth : 300,
							store : examineeApply.SmallTypes
						}, '-', {
							text : '列显示未分配',
							handler : function() {
								examineeApply.lstore.load({
											params : {
												state : 1
											}
										});
							}
						}])
			});
	examineeApply.RPanel = new Ext.grid.GridPanel({
		columnWidth : .65,
		cm : examineeApply.cm,
		frame : true,
		store : examineeApply.store,
		loadMask : true,
		height : 480,
		autoScroll:true,
		viewConfig : {
			columnsText : '选择显示列',
			forceFit : true
		},
		tbar : {
			xtype : 'toolbar',
			autoWidth : true,
			forceLayout : true,
			items : ['-', {
				text : '重新安排',
				handler : function() {
					examineeApply.store.removeAll();
					Ext.Ajax.request({
										url : 'center/clearSessionForExaminee.action',
										method : 'post',
										success : function(response) {
										},
										failure : function(response) {
											Ext.Msg.alert("错误", "服务器连接失败！");
										}
									});
				}
			}, "-", {
				text : "自动分配考生",
				handler : function() {
					examineeApply.autoArrange = true;
					Ext.Ajax.request({
								url : 'center/autoArrangeForExaminee.action',
								method : 'post',
								params : {
									exam_Id : examineeApply.exam_Id
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									if (obj.success == false) {
										Ext.Msg.alert("提示", obj.msg);
									} else {
										Ext.each(
												examineeApply.store.getRange(),
												function(item) {
													item.set("state", "已分配");
												});
										Ext.getCmp("examineeApply_State")
												.setValue("自动分配监考生已经完成");
										Ext.Msg.alert("提示", obj.msg);
										examineeApply.autoArrange = true;
									}
								},
								failure : function(response) {
									Ext.Msg.alert("错误", "服务器连接失败！");
								}
							});
				}
			}, '-', {
				text : '总分配完成确认',
				handler : function() {
					var result = true;
					Ext.each(examineeApply.store.getRange(), function(item) {
								if (item.data.state != "已分配") {
									result = false;
								}
							});
					if (result = true) {
						Ext.Ajax.request({
							url : 'center/confirmArrangeForExaminee.action',
							method : 'post',
							params : {
								time : examineeApply.startTime,
								exam_Id:examineeApply.exam_Id
							},
							success : function(response) {
								var obj = Ext.decode(response.responseText);
								if (obj.success == false) {
									Ext.Msg.alert("提示", obj.msg);
								} else {
									Ext.Msg.alert("提示", obj.msg);
									examineeApply.store.removeAll();
									Ext.getCmp("examineeApply_State")
											.setValue("总分配已经完成");
									Ext.Ajax.request({
										url : 'center/clearSessionForExaminee.action',
										method : 'post',
										success : function(response) {
										},
										failure : function(response) {
											Ext.Msg.alert("错误", "服务器连接失败！");
										}
									});
								}
							},
							failure : function(response) {
								Ext.Msg.alert("错误", "服务器连接失败！");
							}
						});
					} else {
						Ext.Msg.show({
									title : '错误提示',
									msg : '对不起,有考场没有分配监考人员，请仔细检查后提交分配结果！',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.WARNING
								});
					}
				}
			}, '->', "状态栏:", {
				id : 'examineeApply_State',
				xtype : 'displayfield',
				value : "未进行如何操作"
			}]
		}
	});
	examineeApply.Main = new Ext.Panel({
				title : '考生人员安排',
				id : 'examineeApplyMainPanel',
				deferredRender : true,
				closeAction : 'hide',
				cm : examineeApply.cm,
				store : examineeApply.store,
				loadMask : true,
				autoWidth : true,
				closable : true,
				layout : 'column',
				viewConfig : {
					forceFit : true
				},
				items : [examineeApply.LPanel, examineeApply.RPanel]
			});
	Ext.getCmp("examineeApply_search_bigType").on('select', function(combo) {
				examineeApply.SmallTypes.load({
							params : {
								big_Id : combo.getValue(),
								limit : 10
							}
						});
			});
	Ext.getCmp("examineeApply_search_smallType").on('select', function(combo) {
				examineeApply.lstore.load({
							params : {
								small_Id : combo.getValue(),
								state : 0
							}
						});
			});
	examineeApply.LPanel.on('rowdblclick', function(grid, rowindex, event) {
		Ext.Ajax.request({
										url : 'center/clearSessionForExaminee.action',
										method : 'post',
										success : function(response) {
										},
										failure : function(response) {
											Ext.Msg.alert("错误", "服务器连接失败！");
										}
									});
				var record = grid.getStore().getAt(rowindex);
				examineeApply.startTime = record.get("l_time");
				examineeApply.endTime = record.get("l_etime");
				examineeApply.exam_Id = record.get("l_id");
				examineeApply.store.load({
							params : {
								exam_Id : examineeApply.exam_Id
							}
						});
			});
	examineeApply.menu = new Ext.menu.Menu({
				id : 'examineeApply_Menu',
				items : [{
							id : 'examineeApplyMenu',
							handler : function() {
								examineePanelApply.Main.show();
								examineePanelApply.lstore.load({
											params : {
												unit_Id : examineeApply.unit_Id
											}
										});
							}
						}]
			});
	examineeApply.RPanel.on("rowcontextmenu", function(grid, rowIndex, e) {
				if (examineeApply.autoArrange == false) {
						Ext.Msg.show({
									title : '提示',
									msg : '对不起,只有进行自动安排考生才能使用右键来查询相应考场的考生安排！',
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.WARNING
								});
				} else {
					e.preventDefault();
					examineeApply.RPanel.getSelectionModel()
							.selectRow(rowIndex);
					examineeApply.menu.showAt(e.getXY());
					examineeApply.menuClick = examineeApply.RPanel
							.getSelectionModel().getSelected(rowIndex);
					examineeApply.unit_Id = examineeApply.menuClick
							.get("epq_Id");
					Ext.getCmp("examineeApplyMenu").setText("'"
							+ examineeApply.menuClick.get("name") + "'考点考生查询");
				}
			});
};
Ext.onReady(examineeApply.init);